package org.eu.hdeogratias.license.config;

import org.eu.hdeogratias.license.operation.LicenseVerify;
import org.eu.hdeogratias.license.property.LicenseProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@EnableConfigurationProperties(value = {LicenseProperty.class})
public class LicenseConfiguration {
    private final static Logger logger = LoggerFactory.getLogger(LicenseConfiguration.class);
    private final LicenseProperty licenseProperty;

    public LicenseConfiguration(LicenseProperty licenseProperty) {
        this.licenseProperty = licenseProperty;
    }

    @Bean(initMethod = "installLicense", destroyMethod = "unInstallLicense")
    @ConditionalOnMissingBean(value = LicenseVerify.class)
    public LicenseVerify licenseVerify() {
        LicenseVerify licenseVerify = new LicenseVerify(licenseProperty.getSubject(), licenseProperty.getPublicAlias(), licenseProperty.getStorePass(), licenseProperty.getLicensePath(), licenseProperty.getPublicKeysStorePath());
        return licenseVerify;
    }
}
